using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Windows.Forms;

namespace _3D_FileExplorer
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class KeyboardControl
    {
        int lastIndex = 0;
        int up = 0;
        int down = 0;
        int left = 0;
        int right = 0;
        int back = 0;
        Boolean enter = false;
        public KeyboardControl()
        {
           Fe.canvas.KeyDown += new KeyEventHandler(Panel_KeyPress);
            // TODO: Construct any child components here
        }

        void Panel_KeyPress(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.W)||(e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                up++;
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.S)||(e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                down++;
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.D) || (e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                right++;
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.A) || (e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                left++;
            }
            if (e.KeyCode == System.Windows.Forms.Keys.Enter)
            {
                enter = true;
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Q) || (e.KeyCode == System.Windows.Forms.Keys.Back))
            {
                back++;
            }
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        
        public Primitives LeafUpdate(List<Primitives> leafArray,string rootPath, out int load)
        {
            // TODO: Add your update code here
            // Go up
            load = 2;
          if (up > 0)
            {
                if (lastIndex < (leafArray.Count - 1))
                {
                    lastIndex += 1;
                    load = 2;
                }
                else
                {
                    load = 1;
                }
                up = 0;
                return leafArray[lastIndex];
            }

            // Go down
            if (down >0)
            {
                if (lastIndex > 0)
                {
                    lastIndex -= 1;
                    down = 0;
                    load = 2;
                    return leafArray[lastIndex];
                }
                else
                {
                    load = 0;
                    down = 0;
                    ResetCounter(0); 
                    return leafArray[0];
                }
            }

            if (enter == true)                  
            {
                ((LeafPrimitive)leafArray[lastIndex]).opened = true;
                enter = false;
                return leafArray[lastIndex];
            }

            if (back > 0)
            {
                back = 0;
                DrawGraphicsTree.CreateNewThread(Fe.graphics.GraphicsDevice,rootPath, 0,true);
            }
            return leafArray[lastIndex];
        }

        public long Update(GraphicalNode branch)
        {
            // TODO: Add your update code here
            // Go up
            if (up > 0)
            {
                if (lastIndex < (branch.Branch.Count - 1))
                {
                    lastIndex += 1;

                }
                up = 0;
                return branch.Branch[lastIndex].id;
            }

            // Go down
            if (down > 0)
            {
                if (lastIndex > 1)
                {
                    lastIndex -= 1;
                    down = 0;
                    return branch.Branch[lastIndex].id;
                }
                else
                {
                    lastIndex = 0;
                    down = 0;
                    return branch.Branch[0].id;
                }
            }

            if (enter == true)
            {
                //ResetCounter();
                DrawGraphicsTree.CreateNewThread(Fe.graphics.GraphicsDevice);
                enter = false;
                Interface.messageBoard = "";
            }
            if (back > 0)
            {
                back = 0;
                DrawGraphicsTree.CreateNewThread(Fe.graphics.GraphicsDevice, Fe.par.defaultBranchLevel);
                Interface.messageBoard = "";
            }
                return branch.Branch[lastIndex].id;
        }

        public void LastBranch()
        {
            if (back > 0)
            {
                back = 0;
                DrawGraphicsTree.CreateNewThread(Fe.graphics.GraphicsDevice, Fe.par.defaultBranchLevel);
                Interface.messageBoard = "";
            }
        }

        public void ResetCounter(int index)
        {
            lastIndex = index;
            up = 0;
            down = 0;
            left = 0;
            right = 0;
            enter = false;
            back = 0;
        }
    }
}